$(function () {
    log_detail.init();
})

/**
 * 日志详情--js
 * @author lmz
 */
var log_detail = {
    _ID: null,
    _OBJ: null,
    init: function () {
        var obj = GetRequest();
        log_detail._ID = obj.id;

        log_detail.findById(log_detail._ID);
    },
    /**
     * 根据id查询详情
     */
    findById: function (id) {
        $.ajax({
            url: "/crm/dataLog/findById",
            dataType: "json",
            async: true,
            data: {
                id: id
            },
            type: "post",
            success: function (data) {
                log_detail._OBJ = data;
                //$("#userUuid").text(data.userUuid);
                $("#userName").text(data.userName);
                //$("#dataTuid").text(data.dataTuid);
                //$("#dataTitle").text(data.dataTitle);
                $("#operateRemarks").text(data.operateRemarks);
                $("#logType").text(data.logType);
                $("#operateTime").text(data.operateTime);
                $("#createUserName").text(data.createUserName);
                $("#logType").text(data.logType == 0 ? "企业日志" : "项目日志");
                log_detail.checkLog(data)

            },
            error: function () {
                layer.msg("系统错误！", {icon: 2, closeBtn: 0});
                ;
            }
        });
    },
    /**
     * 对项目，企业资源进行修改内容对比
     */

    checkLog: function (data) {
        //console.log(data);
        //企业资源的对比
        //原始内容
        var originalContent = data.originalContent;

        //更改后的内容
        var latestContent = data.latestContent;
        //console.log(latestContent)
        //如果原始内容为空，则是新增   如果更改内容为空，则是删除,显示新增或者删除
        if (isBlank(data.originalContent)) {
            //原始内容=更新内容
            originalContent = latestContent;
            originalContent = JSON.parse(originalContent);
            latestContent = JSON.parse(latestContent);
            //企业新增内容显示操作
            if (data.logType == 0) {
                $("#project").hide();
                $("#progress").hide();
                $("#accessory").hide();
                if(data.detailedType==0) {
                    log_detail.companyData(originalContent, latestContent);
                    $("#originalContent h3").text("企业新增内容")
                    $("#company").show();
                    $("#company #originalContent").show();
                    $("#company #latestContent").hide();
                }
                //企业维护进度新增
                if (data.detailedType==1) {
                    //console.log(originalContent)
                    $("#progress h3").text("企业维护进度新增")
                    log_detail.progress(originalContent,latestContent);
                    $("#progress").show();
                }
                //企业附件变更
                if (data.detailedType==2) {
                    //console.log(originalContent)
                    $("#accessory h3").text("企业附件新增")
                    log_detail.accessory(originalContent,latestContent);
                    $("#accessory").show();

                }
            }
            //项目新增内容显示操作
            if (data.logType == 1) {
                $("#progress").hide();
                $("#company").hide();
                $("#accessory").hide();
                if(data.detailedType==3) {
                    log_detail.projectData(originalContent, latestContent);
                    $("#projectOriginalContent h3").text("项目新增内容")
                    $("#project").show();
                    $("#project #projectOriginalContent").show();
                    $("#project #projectLatestContent").hide();
                }
                //项目维护进度新增
                if (data.detailedType==4) {
                    $("#progress h3").text("项目维护进度新增")
                    //console.log(originalContent)
                    log_detail.progress(originalContent,latestContent);
                    $("#progress").show();

                }
                //项目方案变更
                if (data.detailedType==5) {
                    $("#accessory h3").text("项目方案新增")
                    //console.log(originalContent)
                    log_detail.accessory(originalContent,latestContent);
                    $("#accessory").show();

                }

            }
            //do something
        }
        if (isBlank(data.latestContent)) {
            //原始内容=更新内容
            latestContent = originalContent;
            originalContent = JSON.parse(originalContent);
            latestContent = JSON.parse(latestContent);
            //企业内容显示操作
            if (data.logType == 0) {
                if(data.detailedType==0){
                $("#project").hide();
                log_detail.companyData(originalContent, latestContent);
                $("#originalContent h3").text("企业变更内容")
                $("#company").show();
                $("#company #originalContent").show();
                    $("#company #latestContent").hide();
                }
            }
            //项目新增内容显示操作
            if (data.logType == 1) {
                if(data.detailedType==3){
                    $("#company").hide();
                    log_detail.projectData(originalContent, latestContent);
                    $("#projectOriginalContent h3").text("项目变更内容")
                    $("#project").show();
                    $("#project #projectOriginalContent").show();
                    $("#project #projectLatestContent").hide();
                }

            }
        }


        //如果原始内容和更改内容都不为空，则是新增，进行比较
        if (!isBlank(data.originalContent) && !isBlank(data.latestContent)) {
            // console.log("更改对比")
            originalContent = JSON.parse(data.originalContent);
            latestContent = JSON.parse(data.latestContent);
            //console.log(originalContent.entName)
            //logType为0经行企业比较，不为0进行项目比较
            //企业基本资料变动比较
            if (data.logType == 0) {
                //console.log("企业更改对比")
                log_detail.companyData(originalContent, latestContent)
                $("#latestContent").append($("#originalContent .one").clone(true))
                $("#latestContent h3").text("更新后的内容")
                //比对两个json字符串的不同
                var different = log_detail.comparedjson(originalContent, latestContent)
                //判断结果集是否为空
                if (!$.isEmptyObject(different)) {
                    for (var s in different) {
                        //由于返回的行业大类也是一个json的字符串所以还需处理
                        if (s == "entIndustryOne" && !isBlank(different[s])) {
                            var lastIndustry = JSON.parse(different[s]);

                            $("#latestContent ." + s).text(lastIndustry.indName)
                            $("#latestContent ." + s).css("color", "red")
                            $("#latestContent ." + s).parent().find("dt").css("color", "red")
                            $("#originalContent ." + s).css("color", "red")
                            $("#originalContent ." + s).parent().find("dt").css("color", "red")
                        } else {
                            $("#latestContent ." + s).text(different[s])
                            $("#latestContent ." + s).css("color", "red")
                            $("#latestContent ." + s).parent().find("dt").css("color", "red")
                            $("#originalContent ." + s).css("color", "red")
                            $("#originalContent ." + s).parent().find("dt").css("color", "red")
                        }
                    }
                }
                else {
                    // $("#company ").hide();
                    $("#company #originalContent").hide();


            }
            }
            //项目基本资料变动比较
            else if (data.logType == 1) {
                //console.log("项目更改对比")
                log_detail.projectData(originalContent, latestContent)
                $("#projectLatestContent").append($("#projectOriginalContent .one").clone(true));
                $("#projectLatestContent .one h3").text("更新后的内容")
                //比对两个json字符串的不同
                var projectDifferent = log_detail.comparedjson(originalContent, latestContent);

                //判断返回的不同结果集是否为空，空则没有变动
                if (!$.isEmptyObject(projectDifferent)) {
                    //循环不同并赋值
                    for (var p in projectDifferent) {
                        $("#projectLatestContent ." + p).text(projectDifferent[p])
                        $("#projectLatestContent ." + p).css("color", "red")
                        $("#projectLatestContent ." + p).parent().find("dt").css("color", "red")
                        $("#projectOriginalContent ." + p).css("color", "red")
                        $("#projectOriginalContent ." + p).parent().find("dt").css("color", "red")
                    }
                }
                else {
                    // $("#project").hide();
                    $('#project #projectOriginalContent').hide();
                }

            }

        }

    },
    /**
     * json  对比
     * @param json1
     * @param json2
     */

    comparedjson: function (json1, json2) {
        var different = {};
        //循环遍历其中一个json对象
        for (var key in json1) {
            if (typeof (json1[key]) != "object" || json1[key] == null) {
                if (json2[key] != null) {

                    if (json1[key] != json2[key]) {

                        different[key] = json2[key];
                        //console.log('发现不同' + key + '：' + json1[key], json2[key]);
                        //delete json2[key];
                    }

                }

                else {
                    layer.alert("系统开小差了！！请联系管理员");

                    console.log('发现删除' + key + '：' + json1[key], json2[key]);

                }

            }

            else {

                console.log(json1[key].length);

                if (json1[key].length >= 0) {

                    for (i = 0; i < json1[key].length; i++) {


                        this.comparedjson(json1[key][i], json2[key][i]);

                    }

                }

                else {

                    this.comparedjson(json1[key], json2[key]);

                }

            }

        }
        //console.log(different);
        return different;

    },
    /**
     * 企业基本数据渲染
     */
    companyData: function (originalContent, latestContent) {
        //console.log("ssssss" + originalContent)
        $("#project").hide();
        $("#progress").hide();
        $("#accessory").hide();
        $("#originalContent .entName").text(originalContent.entName)
        $("#originalContent .entAddress").text(originalContent.entAddress)
        $("#originalContent .entOnePerson").text(originalContent.entOnePerson)
        $("#originalContent .entOnePhone").text(originalContent.entOnePhone)
        $("#originalContent .entOneBirthday").text(originalContent.entOneBirthday)
        $("#originalContent .entTwoPerson").text(originalContent.entTwoPerson)
        $("#originalContent .entTwoPhone").text(originalContent.entTwoPhone)
        $("#originalContent .entTwoBirthday").text(originalContent.entTwoBirthday)
        //提取行业大类的名称
        if (!isBlank(originalContent.entIndustryOne)) {
            var industry = JSON.parse(originalContent.entIndustryOne)
            var orIndName = industry.indName;
            $("#originalContent .entIndustryOne").text(orIndName)
            //console.log("123sadas"+industry.indName)
        }
        $("#originalContent .entIndustryTwo").text(originalContent.entIndustryTwo)
        $("#originalContent .entUpstream").text(originalContent.entUpstream)
        $("#originalContent .entDownstream").text(originalContent.entDownstream)
        $("#originalContent .entStockRight").text(originalContent.entStockRight)
        $("#originalContent .entProduct").text(originalContent.entProduct)
        $("#originalContent .entMode").text(originalContent.entMode)
        $("#originalContent .entNeedsUrgent").text(originalContent.entNeedsUrgent)
        $("#originalContent .entNeedsCommonly").text(originalContent.entNeedsCommonly)
        $("#originalContent .createUserName").text(originalContent.createUserName)
        $("#originalContent .liableUserName").text(originalContent.liableUserName)
        $("#originalContent .searchPowerUserName").text(originalContent.searchPowerUserName)
        $("#originalContent .editPowerUserName").text(originalContent.editPowerUserName)
        $("#originalContent .createTime").text(originalContent.createTime)
        $("#originalContent .lastEditTime").text(originalContent.lastEditTime)
        $("#originalContent .entStats").text(originalContent.entStats==0?"正常":"终止")

    },
    /**
     * 项目基本数据渲染
     * @param originalContent
     * @param latestContent
     */
    projectData: function (originalContent, latestContent) {
        //console.log(originalContent)
        $("#company").hide();
        $("#progress").hide();
        $("#accessory").hide();
        $("#projectOriginalContent .proName").text(originalContent.proName);
        $("#projectOriginalContent .entName").text(originalContent.entName);
        $("#projectOriginalContent .liableUserName").text(originalContent.liableUserName);
        $("#projectOriginalContent .createUserName").text(originalContent.createUserName);
        $("#projectOriginalContent .createTime").text(originalContent.createTime);
        $("#projectOriginalContent .beginTime").text(originalContent.beginTime);
        $("#projectOriginalContent .nextStepTime").text(originalContent.nextStepTime);
        $("#projectOriginalContent .teamMembersNames").text(originalContent.teamMembersNames);
        $("#projectOriginalContent .searchPowerUserName").text(originalContent.searchPowerUserName);
        $("#projectOriginalContent .editPowerUserName").text(originalContent.editPowerUserName);
        $("#projectOriginalContent .proStats").text(originalContent.proStats==0?"正常":"终止");
        $("#projectOriginalContent .proRemarks").text(originalContent.proRemarks);
        //$("#projectLatestContent").append($("#projectOriginalContent").clone(true));


    },
    /**
     * 项目维护进度新增数据
     */
    progress:function (originalContent,latestContent) {
        console.log(originalContent)
        $("#company").hide();
        $("#project").hide();
        $("#accessory").hide();
        $("#progress .proTitle").text(originalContent.proTitle);
        $("#progress .proFilePath").text(originalContent.proFilePath);
        $("#progress .proType").text(originalContent.proType==1?"企业":"项目");
        $("#progress .dataName").text(originalContent.dataName);
        $("#progress .proDescribe").text(originalContent.proDescribe);
        $("#progress .createUserName").text(originalContent.createUserName);
        $("#progress .searchPowerUserName").text(originalContent.searchPowerUserName);
        $("#progress .editPowerUserName").text(originalContent.editPowerUserName);
        $("#progress .readStats").text(originalContent.readStats==0?"未读":"已读");
        $("#progress .proStats").text(originalContent.proStats==0?"正常":"终止");
        $("#progress .createTime").text(originalContent.createTime);
        $("#progress .nextStepTime").text(originalContent.nextStepTime);

    },
    /**
     * 方案或者附件并更
     */

    accessory:function (originalContent,latestContent) {
        $("#company").hide();
        $("#project").hide();
        $("#progress").hide();
        $("#accessory .accTitle").text(originalContent.accTitle);
        $("#accessory .accFilePath").text(originalContent.accFilePath);
        $("#accessory .accType").text(originalContent.accType==1?"企业":"项目");
        $("#accessory .createTime").text(originalContent.createTime);
        $("#accessory .userName").text(originalContent.createUserName);
        $("#accessory .planDescribe").text(originalContent.planDescribe);

    }
}

	